<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/carts.css') }}">
    <script src="{{ url_for('static', filename='js/cart.js') }}"></script>
</head>
<body>
    <!-- 导航栏 -->
    <nav>
        <ul>
            <li><a href="/">首页</a></li>
            <li><a href="/products">产品</a></li>
            <li><a href="/cart">购物车</a></li>
            <li><a href="/orders">我的订单</a></li>
            <li><a href="/about">关于我们</a></li>
            <li><a href="/contact">联系我们</a></li>
            {% if 'username' in session %}
            <li><a href="/profile">个人资料</a></li>
            <li><a href="/logout">登出</a></li>
            {% else %}
            <li><a href="/login">登录</a></li>
            <li><a href="/register">注册</a></li>
            {% endif %}
        </ul>
    </nav>

    <!-- 主内容 -->
    <div class="main-content">
        <h2>购物车</h2>
        <form action="{{ url_for('place_order') }}" method="post">
    {% if cart_items %}
    <table>
        <thead>
            <tr>
                <th>选择</th>
                <th>产品图片</th>
                <th>产品名称</th>
                <th>描述</th>
                <th>数量</th>
                <th>单价</th>
                <th>总价</th>
                <th>库存</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% set total_amount = 0 %}
            {% for item in cart_items %}
            {% set item_total = item[3] * item[5] %}
            <tr>
                <td><input type="checkbox" name="product_ids" value="{{ item[0] }}" class="cart-item-checkbox" data-item-price="{{ item[3] }}" data-item-quantity="{{ item[5] }}"></td>
                <td><img src="data:image/jpeg;base64,{{ item[4] | b64encode }}" alt="{{ item[1] }}" width="100"></td>
                <td>{{ item[1] }}</td>
                <td>{{ item[2] }}</td>
                <td>
                    <input type="number" name="quantity" value="{{ item[5] }}" min="1" max="{{ item[6] }}" class="quantity-input" data-item-id="{{ item[0] }}">
                    <button type="button" class="update-button" data-item-id="{{ item[0] }}" data-url="{{ url_for('update_cart') }}">更新</button>
                </td>
                <td>￥{{ item[3] }}</td>
                <td>￥<span class="item-total">{{ item_total }}</span></td>
                <td>{{ item[6] }}</td> <!-- 显示库存 -->
                <td>
                    <form action="{{ url_for('delete_from_cart', product_id=item[0]) }}" method="post" style="display: inline;">
                        <button type="submit">删除</button>
                    </form>
                </td>
            </tr>
            {% endfor %}
            <tr>
                <td colspan="6" style="text-align: right;">总金额：</td>
                <td>￥<span id="total-amount">0</span></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <button type="submit">下单</button>
    {% else %}
    <p>您的购物车是空的。</p>
    {% endif %}
</form>


    </div>

    <!-- 页脚 -->
    <footer>
        <p>&copy; 2024 网上商店。保留所有权利。</p>
        <ul>
            <li><a href="/terms">服务条款</a></li>
            <li><a href="/privacy">隐私政策</a></li>
        </ul>
    </footer>
</body>
</html>
